<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>添加页面</title>
    <th:block th:include="material/core/resources::headers"/>
</head>
<body>
    <div>
        <form class="layui-form">
            <div  class="layui-form-item">
                <label for="id"><span>编号</span></label>
                <div><input type="text" id="id"></div>
            </div>
            <div  class="layui-form-item">
              <label for="name"><span>姓名</span></label>
              <div><input type="text" id="name"></div>
            </div>

            <div  class="layui-form-item">
                <label for="age"><span>年龄</span></label>
                <div><input type="text" id="age"></div>
            </div>
            <div  class="layui-form-item">
                <label for="email"><span>邮箱</span></label>
                <div><input type="text" id="email"></div>
            </div>
            <div  class="layui-form-item">
                <label for="createTime"><span>创建时间</span></label>
                <div><input type="text" id="createTime" readonly="readonly" placeholder="系统自动填充"></div>
            </div>
            <div  class="layui-form-item">
                <label for="updateTime"><span>更新时间</span></label>
                <div><input type="text" id="updateTime" readonly="readonly" placeholder="系统自动填充"></div>
            </div>
            <div>
                <button lay-filter="insert" lay-submit="insert"  readonly="readonly">添加</button>
                <button type="reset">重置</button>
            </div>
        </form>
    </div>
</body>
</html>

<script>
    layui.use(['form'],
        function () {
        debugger
            const $ = layui.jquery;
            const form = layui.form, layer = layui.layer;
            form.on('submit(insert)',
                //获取数据
                function () {
                    const a = $("#id").val();
                    const b = $("#name").val();
                    const c = $("#age").val();
                    const d = $("#email").val();
                    const e = $("#createTime").val();
                    const f = $("#updateTime").val();
                    const data = {
                        id: a,
                        name: b,
                        age: c,
                        email: d,
                        createTime:e,
                        updateTime:f,
                    };
                    $.ajax({
                        type: "POST",
                        url: "/spring/user/insertInfo",
                        data: data,
                        async: true,
                        dataType: "json",
                        // 直接将拿到的后台的数据集 在此处定义
                        success: function (backData) {
                            if (backData.code === 1) {
                                layer.alert("增加成功", {
                                    icon: 6
                                }, function () {
                                    xadmin.close();
                                    xadmin.father_reload();
                                });
                                // if 执行完毕之前要 return false 防止跳转
                                return false;
                            } else if (backData.code !== 1) {
                                layer.alert("添加失败", {
                                    icon: 7
                                }, function () {
                                    xadmin.close();
                                    xadmin.father_reload();
                                });
                                return false;
                            }
                        }
                    });
                    return false;
                });
        });
</script>